package com.mobile.web.control;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

import com.mobile.common.base.web.BaseController;
import com.mobile.handler.UsersHandler;
import com.mobile.persistent.entity.Users;

@Controller
@Scope("prototype")
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	@Resource private UsersHandler usersHandler;
	
	@RequestMapping
	public ModelAndView login(
            final HttpServletRequest request,
            final HttpServletResponse response){
		return new ModelAndView("/login.jsp");
	}
	@RequestMapping(params = "action=loading")
	public ModelAndView loading(
            final HttpServletRequest request,
            final HttpServletResponse response){
		final String userName = request.getParameter("userName");
		final String userPwd = request.getParameter("userPwd");
		
		Users user = usersHandler.findByNamePwd(userName, userPwd);
		if(user != null){
			WebUtils.setSessionAttribute(request, "LoginUser", user);
			return new ModelAndView(new RedirectView("index.do"));
		}else{
			return new ModelAndView(new RedirectView("login.do"));
		}
	}
}
